using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// 挂载在Panel上
/// </summary>
public class DrawLine1 : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    public Image image; //画线的Image

    public Image image1; //画线的Image

    public Image image2; //画线的Image

    private Vector2 rectOne;

    //第一个点
    private Vector2 rectTwo;

    //第二个点
    private Vector2 rectThree;

    //第三个点
    private int lineIndex = 0;

    //划线功能
    void DrawStraightLine(Image image, Vector2 a, Vector2 b)
    {
        float distance = Vector2.Distance(a, b);
        //求距离
        float angle = Vector2.SignedAngle(a - b, Vector2.left);
        //求夹角
        image.GetComponent<RectTransform>().anchoredPosition = (a + b) / 2;
        image.GetComponent<RectTransform>().sizeDelta = new Vector2(distance, 5);
        //长度，宽度
        image.transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward);
    }

    void DrawStraightLine1(Image image, Vector2 a, Vector2 b)
    {
        float
            distance = Vector2.Distance(a,
                b);
        //求距离
        float angle = Vector2.SignedAngle(a - b, Vector2.left);
        //求夹角
        image1.GetComponent<RectTransform>().anchoredPosition = (a + b) / 2;
        image1.GetComponent<RectTransform>().sizeDelta = new Vector2(distance, 5);
        //长度，宽度

        image1.transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward);
    }

    void DrawStraightLine2(Image image, Vector2 a, Vector2 b)
    {
        float distance = Vector2.Distance(a, b);
        //求距离
        float angle = Vector2.SignedAngle(a - b, Vector2.left);
        //求夹角
        image2.GetComponent<RectTransform>().anchoredPosition = (a + b) / 2;
        image2.GetComponent<RectTransform>().sizeDelta = new Vector2(distance, 5);
        //长度，宽度
        image2.transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (lineIndex == 0)
        {
            Vector2 startPoint = new Vector2(eventData.position.x - 960, eventData.position.y - 540);
            rectOne = startPoint;
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (lineIndex == 0)
        {
            lineIndex = 1;
        }
        else if (lineIndex == 1)
        {
            lineIndex = 2;
            DrawStraightLine2(image, rectThree, rectOne);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (lineIndex == 0)
        {
            Vector2 endPoint = new Vector2(eventData.position.x - 960, eventData.position.y - 540);
            rectTwo = endPoint;
            DrawStraightLine(image, rectOne, rectTwo);
        }
        else if (lineIndex == 1)
        {
            Vector2 endPoint = new Vector2(eventData.position.x - 960, eventData.position.y - 540);
            rectThree = endPoint;
            DrawStraightLine1(image, rectTwo, rectThree);
        }
    }
}